import router from './index'
import {getRoutesConfig} from "@/services/user";
import store from '@/store'

const whiteList = ['/login', '/403']// no redirect whitelist

router.beforeEach((to, from, next) => {
    if (to.meta.title) {

    }
    //console.log(to.path);
    if (whiteList.indexOf(to.path) !== -1){
        next();
        return false;
    }else {
        const routes = store.getters['account/routes']
        if (!routes){
            store.commit('account/setRoutes', routes)
            loadRoutes(next, to);
        }else {
            next()
        }
    }

})


router.afterEach(() => {

})


/**
 * 加载动态路由
 * @param next
 * @param to
 */
function loadRoutes(next, to){
    getRoutesConfig().then(result => {
        const links = result.data.data
        //router.addRoutes(links)
        const rootRoute = router.options.routes.find(item => item.path === '/')
        let menuRoutes = rootRoute && rootRoute.children
        if (menuRoutes) {
            store.commit('setting/setMenuData', menuRoutes)
        }
        console.log(menuRoutes);
        next({ ...to, replace: true });

    })
}
